<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>农汇购-商品类型列表</title>
<link href="${pageContext.request.contextPath }/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath }/layui/css/layui.css" rel="stylesheet">
<script src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath }/layui/layui.js"></script>
<script type="text/html" id="barEdit">
    <a class="layui-btn layui-btn-xs" lay-event="delt">删除</a>
</script>
</head>
<body>
	<blockquote class="layui-elem-quote news_search">
	    <form class="layui-form">
	        <div>
	            <div class="layui-inline">
	                <input type="text" id="typename" value="" placeholder="请输入类型名称"
	                       class="layui-input search_input">
	            </div>
	            
	            <a class="layui-btn search_btn" lay-submit="" data-type="search"
	               lay-filter="search">查询</a>
	        </div>
	
	    </form>
	</blockquote>
	<div class="layui-form">
	    <table id="typeList" lay-filter="typeList"></table>
	    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		</fieldset>
	</div>
</body>
 
<script type="text/html" id="flagT">
  <input type="checkbox" name="flag_{{d.id}}" value="{{d.flag}}" lay-filter="flagD" lay-skin="switch" lay-text="启用|禁用" {{ d.flag == 0 ? '' : 'checked' }} />
</script>
<script type="text/javascript">
function parseD(d){
	var date = new Date(d); 
	var dd = date.getDate();
	dd = dd < 10 ? "0" + dd : dd;
	var m = (date.getMonth() + 1);
	m = m < 10 ? "0" + m : m;
	return date.getFullYear()+"-"+m+"-"+ dd;
}
layui.config({
    base: "js/"
}).use(
    ['form', 'layer', 'jquery', 'table', 'laydate','upload'],
    function () {
        var form = layui.form, table = layui.table, layer = parent.layer === undefined ? layui.layer
            : parent.layer,laypage = layui.laypage
            $ = layui.jquery,
            active = {
                search: function () {
                    var typename = $('#typename');
                    //执行重载
                    table.reload('typeList', {
                        page: {
                            curr: 1
                        },
                        where: {
                        	typename: typename.val()
                        }
                    });
                }
            };

        //加载页面数据
        table.render({
            id: 'typeList',
            elem: '#typeList',
            url: "/rj2002/admin/getTypeList",
            limit: 5,
            limits: [5, 10],
            editTrigger: 'dblclick',
            cols: [[ //表头
                {
                    field: 'id',
                    title: 'ID',
                    width:70,
                    sort: true
                },
                {
                    field: 'typename',
                    title: '类型名称',
                    edit: 'text',
                    width:110
                },
                {
                    field: 'typelevel',
                    title: '级别',
                    width:70,
                    templet:function(d){
                    	return d.typelevel==0?"父级":"子级"
                    }
                },
                {
                    field: 'flag',
                    title: '状态',
                    width:110,
                    templet: '#flagT', 
                    unresize: true
                },
                {
                    field: 'createtime',
                    title: '创建时间',
                    width:110,
                    templet:function(d){return parseD(d.createtime)}
                },
                {
                    title: '操作',
                    toolbar: '#barEdit'
                }
            ]],
            page: {
            	first: '首页'
                ,last: '尾页'
                ,prev: '<em>←</em>'
                ,next: '<em>→</em>'
				,theme: '#1E9FFF'
            },
            parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
             return {
                 "code": res.code, //解析接口状态
                 "msg": res.msg, //解析提示文本
                 "count": res.count, //解析数据长度
                 "data": res.data //解析数据列表
               };
             }
            //, where: {}
            //开启分页
        });
        
        form.on('switch(flagD)', function(obj){
            console.log(this.name+"---------"+this.value+"---"+obj.elem.checked)
            $.ajax({
				url:"/rj2002/admin/updateTypeFlag",
				type:"post",
				dataType:"json",
				data:{
					'id':this.name.substring(5,this.name.length),
					'flag':obj.elem.checked?1:0
				},
				success:function(data){ 
					layer.alert(data.data,{icon: 1},function(index){ window.location.reload(); });
				},
				error:function(data){ 
					console.log(data)
					layer.msg(data.data) 
				}
			});              
        });
        
        
       
        //监听工具条
        table.on('tool(typeList)', function (obj) {
            var data = obj.data;
            if(obj.event === 'delt'){
            	layer.confirm('纳尼？真的要删除账号：'+data.account, {
           		  btn: ['yes', 'no'] //可以无限个按钮
           		}, function(index, layero){
           		  	//调用删除
           		  	$.ajax({
						url:"/rj2002/admin/deleteTypeById",
						type:"post",
						dataType:"json",
						data:{
							'id':data.id
						},
						success:function(data){ 
							layer.alert(data.data,{icon: 1},function(index){ window.location.reload(); });
						},
						error:function(data){ layer.msg(data.data) }
					});
           		}, function(index){
           		  	layer.close(index)
           		});
            	
            }
        });
        
        
        table.on('edit(typeList)', function(obj){
            var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
            console.log('[ID: '+ data.id +'] ' + field + ' 字段更改值为：'+ value);
            $.ajax({
				url:"/rj2002/admin/editTypename",
				type:"post",
				dataType:"json",
				data:{
					'id':data.id,
					'typename':value
				},
				success:function(data){ 
					layer.alert(data.data,{icon: 1},function(index){ window.location.reload(); });
				},
				error:function(data){ layer.msg(data.data) }
			});
          });
        
        //查询
        $(".search_btn").click(function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        })

    })

</script>



</html>